package com.wfs.suanfa.nio.demo.echo.nonio;

import lombok.SneakyThrows;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class PlainOioClient {
    @SneakyThrows
    public static void client(int port){
        Socket socket=new Socket("127.0.0.1",port);

            System.out.println("client  from "+socket);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    InputStream inputStream= null;
                    try {
                         inputStream = socket.getInputStream();
                        byte buffer[] = new byte[1024 * 4];
                        inputStream.read(buffer);
                        String s = inputStream.toString();
                        System.out.println(new String(buffer));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }).start();
        }

    public static void main(String[] args) {
        client(7001);
    }
}


